/*
 * @Author: 曹捷
 * @Date: 2019-08-02 10:11:43
 * @LastEditors: 曹捷
 * @LastEditTime: 2020-11-29 23:56:18
 * @Description: 系统通用过滤器
 */
import moment from 'moment'
import numeral from 'numeral'

export function num (value, fmt = '0.00', tofmt) {
  if (value !== null) {
    return numeral(value).format(fmt)
  } else {
    return value
  }
}
export function valueType (value, filterType) {
  if (filterType) {
    if (value !== null) {
      if (filterType === 'numMoney') {
        return numeral(value).format('0.00')
      } else if (filterType === 'num') {
        return numeral(value).format('0')
      } else if (filterType === 'date') {
        if (value) {
          return moment(value).format('YYYY-MM-DD')
        } else {
          return value
        }
      } else if (filterType === 'time') {
        if (value) {
          return moment(value).format('YYYY-MM-DD HH:ss')
        } else {
          return value
        }
      } else if (filterType === 'dateM') {
        let m = moment(value)
        if (m._isValid) {
          return m.format('YYYYMM')
        } else {
          return value
        }
      } else if (filterType === 'rate') {
        if (value === undefined) {
          return null
        }
        return numeral(value).format('0.00%')
      } else if (filterType === 'rate1') {
        return `${value}%`
      } else if (filterType === 'rate2') {
        if (isNaN(value)) {
          return value
        }
        return numeral(value / 100).format('0.00%')
      }
    }
    return ''
  } else {
    return value
  }
}


export function dateFormat (value, fmt = 'YYYY-MM-DD', tofmt) {
  if (tofmt) {
    return moment(value, fmt).format(tofmt)
  } else {
    return moment(value).format(fmt)
  }
}
export function sexName (value) {
  return value === 1 ? '男' : '女'
}
export function typeName (value) {
  let info = { 1: '文章', 2: '问题', 3: '知识', 4: '动态' }
  return info[value] ? info[value] : value
}
export function timeLong (value) {
  let time = new Date(value)
  let nows = new Date()

  let mis = (nows.getTime() - time.getTime()) / 1000.0 / 60.0
  let data = mis / 60
  if (data / 24 > 365) {
    value = `${Math.floor(data / 24 / 365)}年前`
  } else if (data / 24 / 30 > 6) {
    value = '半年前'
  } else if (data / 24 > 30) {
    value = `${Math.floor(data / 24 / 30)}个月以前`
  } else if (data / 24 > 15) {
    value = '半个月以前'
  } else if (data / 24 > 1) {
    value = `${Math.floor(data / 24)}天以前`
  } else if (data > 1) {
    value = `${Math.floor(data)}小时以前`
  } else if (mis > 30) {
    value = '半个小时以前'
  } else if (mis > 1) {
    value = `${Math.floor(mis)}分钟以前`
  } else {
    value = '刚刚发布'
  }
  return value
}
